const fs = require("fs");
const path = require("path");
const funTool = require("../../tool/funTool");
const { sys_log, op } = require("../../middleware/baseModel");
module.exports = {
  "GET /sys_log/all": async (ctx, next) => {
    let folderPath = path.join(__dirname, `../../logs`);
    let files = fs.readdirSync(folderPath);
    let newFiles = files.reverse().map((p) => {
      return {
        title: p,
      };
    });
    return ctx.success(newFiles);
  },

  "GET /sys_log/detail": async (ctx, next) => {
    let title = ctx.get("title");
    let filePath = path.join(__dirname, `../../logs/${title}`);
    let data = fs.readFileSync(filePath, "utf-8");

    // data = data.replace(/\n/g, "</br>");
    return ctx.success(data);
  },

  "GET /sys_log/delete": async (ctx, next) => {
    let title = ctx.get("title");
    let filePath = path.join(__dirname, `../../logs/${title}`);

    if (await funTool.isExist(filePath)) {
      fs.unlinkSync(filePath);
    }
    return ctx.success();
  },

  "GET /sys_log/operates": async (ctx, next) => {
    let param = ctx.getPageSize();
    let data = await sys_log.findAndCountAll({ order: [["id", "desc"]], ...param });
    return ctx.success(data);
  },
};
